package org.jackysoft.service.impl;

import java.util.Collection;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.jackysoft.data.AbstractDataProvider;
import org.jackysoft.entity.SecurityRole;
import org.jackysoft.entity.TreeNode;
import org.jackysoft.entity.WebModule;
import org.jackysoft.mapper.TreeNodeMapper;
import org.jackysoft.service.SecurityRoleService;
import org.jackysoft.service.TreeNodeService;
import org.jackysoft.service.WebModuleService;
import org.jackysoft.ztree.TreeNodeZtreeFunction;
import org.jackysoft.ztree.ZTreeNode;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import com.google.common.base.Joiner;
import com.google.common.collect.Collections2;
import com.google.common.collect.Lists;

@Service
public class TreeNodeServiceImpl extends
		AbstractDataProvider<TreeNode> implements TreeNodeService {

	@Resource
	TreeNodeMapper mapper;
	
	@Resource
	private WebModuleService modelService;
	
	@Resource
	private SecurityRoleService securityRoleService;
	
	
	public TreeNodeServiceImpl() {
		super();
	}

	@Override
	@SuppressWarnings("unchecked")
	public Collection<TreeNode> getsSimpled() {
		Collection<TreeNode> list = mapper.findFromRoot();
		return list;
	}

	@Override	
	public Collection<TreeNode> getsByParentAndCurrentUser(String parentId) {
		
		Collection<TreeNode> alist = mapper.findByParentForUser(parentId, currentPermissions());
		return alist;

	}

	/**
	 * 返回所有资源,用于权限分配
	 * */
	@Override
	public Collection<TreeNode> getsByParent(String parentId)
			throws DataAccessException {
		Collection<TreeNode> list = mapper.findByParent(parentId);
		return list;
	}

	/**
	 * 获得当前用户权限所分配的树节点
	 * 
	 * @param parentId
	 *            父ID
	 * @param acegi
	 *            角色编号
	 * @param htpsession
	 *            HttpSession
	 * @return Collection<TreeNode> 符合树节点集合
	 * */
	@Override
	public Collection<TreeNode> getsByParentAndSecurole(String parentId,
			String role) throws DataAccessException {
		Collection<TreeNode> alist = mapper.findByParentForSecurityRole(parentId, role);
		return alist;
	}

	
	
	@Override
	public Collection<TreeNode> getsByCurrentUser(String parentId)
			throws DataAccessException {
		
		       
        String context = System.getProperties().get("contextPath").toString();

		WebModule wm = modelService.find(StringUtils.trim(context));
		int ctype = wm!=null?wm.getContextType():0;
        
        Collection<TreeNode> trees = mapper.findByParentForSecurityRoleAndContext(parentId, currentPermissions(),ctype);
		return trees;	
		
	}

	@Override
	public Collection<TreeNode> getsByCurrentUser() throws DataAccessException {
		
        Collection<TreeNode> alist= mapper.findAllForSecurityRoles(currentPermissions());
	
		return alist;
	}

	/**
	 * 取得某个权限的所有资源编号
	 * */
	@Override
	public Collection<TreeNode> getsBySecurityRole(String secRole)
			throws DataAccessException {
		Collection<TreeNode> alist = mapper.findAllForSecurityRole(secRole);
		return alist;
	}

	@Override
	public Collection<ZTreeNode> ztree(String id)
			throws DataAccessException {
		if (id == null) {
			id = "root";
		}
		
		Collection<TreeNode> trees = getsByCurrentUser(id);
		return Collections2.transform(trees, new TreeNodeZtreeFunction());
	}

	@Override
	public Collection<TreeNode> getsCheckboxedByParent(String parentId)
			throws DataAccessException {
		mapper.findByParentForUser(parentId, currentPermissions());
		return null;
	}
	
	private String currentPermissions(){
		Subject suser = SecurityUtils.getSubject(); 
		Collection<SecurityRole> oroles = securityRoleService.findEnabledByUser((String) suser.getPrincipal());
        if(oroles==null || oroles.isEmpty())return null;
        List<String> roleNames= Lists.newArrayList();
        for(SecurityRole sr:oroles)roleNames.add(sr.getName());	
        return Joiner.on("','").join(roleNames);
	}

}